define([
	'jquery',
	'underscore',
	'backbone',
	'baseView',
	'text!o2o/login/login.html',
	'js/myUtil.js'
], function($, _, Backbone, BaseView, tpl, myUtil) {
	return BaseView.extend({
		render: function(cb) {
			// check if already login
			var self = this;
			myUtil.isLogined(function(err, res) {
				if (res && res.errCode === 0) {
					console.log('already login');
					window.location.href = "#o2o/login/home.js";
				} else {
					console.log('not login');
					// console.log('test render, qs=%s', qs);
					var tmpl = _.template(tpl);
					$(self.el).html(tmpl);
					if (!$(self.el).is(":visible")) // if hide then show
						self.show();
					cb && cb();
				}
			});
		},

		events: {
			"submit #adminLogin": "doLogin",
		},

		doLogin: function(event) {
			event.preventDefault();
			var formData = $('form#adminLogin').serializeObject();
			console.log(formData);
			var nodeTip = $(this.el).find('.tip');
			nodeTip.hide();

			myUtil.login(formData, function(err, res) {
				console.log(res);
				if (err) return nodeTip.text(err).show();
				if (res.errCode !== 0) return nodeTip.text(res.errMsg).show();

				// all sucess;
				console.log('succ');
				window.location.href = "#o2o/login/home.js";
			});
		}
	});
});